package kd.bos.openapi.sdk.exception;

import kd.bos.openapi.sdk.response.result.ResultCode;
import kd.bos.openapi.sdk.util.StringUtils;

/**
 * @Deacription 异常处理
 * @author johnzou
 * @date 2022年4月6日
 */
public class APIException extends RuntimeException{
    // 错误代码
    private String errorCode;
    // 消息
    private String message;
    private String[] args;

    public static APIException of(ResultCode  resultCode) {
        return new APIException(resultCode.getCode()+"", resultCode.getMessage(), null);
    }
    public static APIException of(ResultCode  resultCode, String message) {
        return new APIException(resultCode.getCode()+"", resultCode.getMessage()+":"+message, null);
    }

    public static APIException of(ResultCode  resultCode, String message, Throwable e) {
        return new APIException(resultCode.getCode()+"", resultCode.getMessage()+":"+message, e);
    }

    public static APIException of(String errorCode, String message, Throwable cause) {
        return new APIException(errorCode, message, cause);
    }

    public static APIException of(String errorCode, String message) {
        return new APIException(errorCode, message);
    }

    public APIException(String errorCode, String message, String[] args, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
        this.message = message;
        this.args = args;
    }

    public APIException(String errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
        this.message = message;
    }

    public APIException(String errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
        this.message = message;
    }

    public String getMessage() {
        if (StringUtils.isEmpty(message)) {
            return "";
        }
        if (args == null || args.length == 0) {
            return "";
        }
        return String.format(message, args);
    }

    public String getErrorCode(){
        return errorCode;
    }


}
